import sys
import popen2
import pycurl
import os

# TODO
# actually use the ignored domains bit

class ReadCallback(object):
	def __init__(self):
		self.contents = ''
	
	def body_cb(self, buf):
		self.contents += buf

class HttpHoneyClient(object):
    def __init__(self, user_agent='"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"'):
        self.ua = user_agent
        self.pages = {}     # for HTML only
        self.res = {}       # for all data
        self.responses = {} # for POST responses
        self.ignores = []

    def __fetch(self, url, method="get", post_data=False, referrer=False):
        """hidden, called from get() or post()"""

        # TODO
        # http_proxy=http://localhost:8118  <-- not needed yet
        # auto-follow location headers (see curl -e)
        reload(sys)
        sys.setdefaultencoding('utf-8')
		c = pycurl.Curl()
		c.setopt(c.FOLLOWLOCATION, 1)
		_cb = ReadCallback()
		c.setopt(c.URL, url)
		c.setopt(c.WRITEFUCTION, _cb.body_cb)
		if post_data and method.lower() == "post": 
			c.setopt(c.UPLOAD, 1)
			c.setopt(c.READFUNCTION, post_data)
			c.setopt(c.INFILESIZE, len(post_data))
        if referrer: 
			c.setopt(c.REFERRER, referrer)
		c.setopt(c.USERAGENT, self.ua)
		c.perform()
		res = _cb.contents
        res = (res[3:] if res.startswith("\xef\xbb\xbf") else res) # "\xef\xbb\xbf" is the head of UTF-8, may cause spidermonkey to crash
        #res = res.decode('utf-8')
        return res

    def get(self, url, referrer=False):
        try: res = self.__fetch(url, method="get", referrer=referrer)
        except Exception: raise
        self.res[url] = res
        return res

    def post(self, url, post_data=False, referrer=False):
        try: res = self.__fetch(url, method="post", post_data=post_data, referrer=referrer)
        except Exception: raise
        self.responses[url] = res
        return res

    def ignore_domain(self, domain):
        self.ignores.append(domain)

    def ignored(self, hostname):
        for i in self.ignores:
            if hostname.endswith(i): return True
        return False